package com.emenu;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.emenu.application.StaticObject;
import com.emenu.bean.BusHitDetail;
import com.emenu.bean.Menu;
import com.emenu.bean.SubMenu;
import com.emenu.bean.User;
import com.emenu.common.Common;
import com.emenu.common.SaveMenuImg;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;


public class FragmentHaveHit extends Fragment  {
	private StaticObject SO;
	TableLayout table;
	Context context;
	private TextView totalamount;
	private TextView vipprice;
	private int c1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.havehit, container, false);
		totalamount = (TextView) v.findViewById(R.id.totalamount);
		vipprice = (TextView) v.findViewById(R.id.vipprice);
		
		table = (TableLayout) v.findViewById(R.id.tablehavehit);
		appendRow(table);

		return v;

	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		SO = (StaticObject) activity.getApplication();

		context = getActivity();

		c1 = getArguments().getInt("c1");

	}

	@Override
	public void onResume() {
		super.onResume();

	}

	private void appendRow(final TableLayout table) {
		double tt = 0;
		double tp = 0;

		final List<BusHitDetail> list = SO.getBusHitDetailList();

		for (final BusHitDetail bhd : list) {
			if (bhd.getRljtype() == c1) {
				tt = tt + bhd.getPrice() * bhd.getAmount();
				tp = tp + bhd.getPrice() * bhd.getAmount();

				final TableRow row = new TableRow(context);

				TextView menu = new TextView(context);
				menu.setText(bhd.getMenuname());
				menu.setPadding(1, 1, 1, 1);
				menu.setTextSize(10);
				menu.setSingleLine(true);
				menu.setEms(6);
				menu.setTextColor(R.color.havecolor);
				menu.setGravity(Gravity.CENTER);

				TextView price = new TextView(context);
				price.setText(String.valueOf(bhd.getPrice()));
				price.setPadding(1, 1, 1, 1);
				price.setTextSize(10);
				price.setTextColor(R.color.havecolor);
				price.setGravity(Gravity.CENTER);

				TextView amount = new TextView(context);
				amount.setText(String.valueOf(bhd.getAmount()));
				amount.setPadding(1, 1, 1, 1);
				amount.setTextSize(10);
				amount.setTextColor(R.color.havecolor);
				amount.setGravity(Gravity.CENTER);

				TextView tag = new TextView(context);
				tag.setText(bhd.getTag_name());
				tag.setPadding(1, 1, 1, 1);
				tag.setTextSize(10);
				tag.setTextColor(R.color.havecolor);
				tag.setGravity(Gravity.CENTER);

				TextView total = new TextView(context);
				total.setText(Math.round(bhd.getAmount_sum() * bhd.getAmount())
						+ "");
				total.setPadding(1, 1, 1, 1);
				total.setTextSize(10);
				total.setTextColor(R.color.havecolor);
				total.setGravity(Gravity.CENTER);

				Button callmenu = new Button(context);
				callmenu.setText(R.string.callmenu);
				callmenu.setTextSize(10);
				callmenu.setHeight(15);
				callmenu.setWidth(30);
				callmenu.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						bhd.setCalledSingle(1);
					}
				});

				Button edit = new Button(context);
				edit.setText(R.string.edit);
				edit.setTextSize(10);
				edit.setHeight(15);
				edit.setWidth(30);
				edit.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						int series = 0;
						FragmentTransaction ft = getFragmentManager()
								.beginTransaction();

						for (SubMenu su : SO.getSubMenuList()) {
							if (su.getCode().equals(bhd.getMenucode())
									&& su.getIs_Serise() == 1) {
								series = 1;
								break;
							}
						}

						if (series == 1) {
							Common.alertFinish(context, "套餐只能删除，不能修改！");
							/**
							FragmentSetMeal Hit = new FragmentSetMeal();
							Bundle args = new Bundle();
							args.putString("code", bhd.getMenucode());
							args.putDouble("amount", bhd.getAmount());
							Hit.setArguments(args);
							ft.replace(R.id.left, Hit);
							***/
						} else {
							FragmentHit Hit = new FragmentHit();
							Bundle args = new Bundle();
							args.putString("code", bhd.getMenucode());
							args.putDouble("amount", bhd.getAmount());
							Hit.setArguments(args);
							ft.replace(R.id.left, Hit);
						}

						ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
						ft.addToBackStack(null);
						ft.commit();
					}
				});

				Button delete = new Button(context);
				delete.setText(R.string.delete);
				delete.setTextSize(10);
				delete.setHeight(15);
				delete.setWidth(30);
				delete.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
					 
						table.removeView(row); 
						for (Menu mu : SO.getMenuList()) {
							if (mu.getCode().equals(bhd.getBig_Menu_code())) {
								mu.setCount(mu.getCount()
										- (int) bhd.getAmount());
								 
							}
							if (bhd.getRljtype() == mu.getC1()) {
								mu.setCount(mu.getCount()
										- (int) bhd.getAmount());
							}
						}
						list.remove(bhd);
						((FragmentMAin) context).refreshRight();

					}
				});
				row.addView(menu, new TableRow.LayoutParams());
				row.addView(price, new TableRow.LayoutParams());
				row.addView(amount, new TableRow.LayoutParams());
				row.addView(tag, new TableRow.LayoutParams());
				row.addView(total, new TableRow.LayoutParams());
				row.addView(callmenu, new TableRow.LayoutParams());
				row.addView(edit, new TableRow.LayoutParams());
				row.addView(delete, new TableRow.LayoutParams());

				table.addView(row, new TableLayout.LayoutParams());
			} else if (c1 < 0) {
				tt = tt + bhd.getPrice() * bhd.getAmount();
				tp = tp + bhd.getPrice() * bhd.getAmount();

				final TableRow row = new TableRow(context);

				TextView menu = new TextView(context);
				menu.setText(bhd.getMenuname());
				menu.setTextSize(10);
				menu.setSingleLine(true);
				menu.setEms(6);
				menu.setPadding(1, 1, 1, 1);
				menu.setTextColor(R.color.havecolor);
				menu.setGravity(Gravity.CENTER);

				TextView price = new TextView(context);
				price.setText(String.valueOf(bhd.getPrice()));
				price.setTextSize(10);
				price.setPadding(1, 1, 1, 1);
				price.setTextColor(R.color.havecolor);
				price.setGravity(Gravity.CENTER);

				TextView amount = new TextView(context);
				amount.setText(String.valueOf(bhd.getAmount()));
				amount.setTextSize(10);
				amount.setPadding(1, 1, 1, 1);
				amount.setTextColor(R.color.havecolor);
				amount.setGravity(Gravity.CENTER);

				TextView tag = new TextView(context);
				tag.setText(bhd.getTag_name());
				tag.setTextSize(10);
				tag.setPadding(1, 1, 1, 1);
				tag.setTextColor(R.color.havecolor);
				tag.setGravity(Gravity.CENTER);

				TextView total = new TextView(context);
				total.setText(bhd.getAmount_sum() * bhd.getAmount() + "");
				total.setTextSize(10);
				total.setPadding(1, 1, 1, 1);
				total.setTextColor(R.color.havecolor);
				total.setGravity(Gravity.CENTER);

				Button callmenu = new Button(context);
				callmenu.setText(R.string.callmenu);
				callmenu.setTextSize(10);
				callmenu.setHeight(15);
				callmenu.setWidth(30);
				callmenu.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						bhd.setCalledSingle(1);
					}
				});

				Button edit = new Button(context);
				edit.setText(R.string.edit);
				edit.setTextSize(10);
				edit.setHeight(15);
				edit.setWidth(30);
				edit.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						int series = 0;
						FragmentTransaction ft = getFragmentManager()
								.beginTransaction();

						for (SubMenu su : SO.getSubMenuList()) {
							if (su.getCode().equals(bhd.getMenucode())
									&& su.getIs_Serise() == 1) {
								series = 1;
								break;
							}
						}

						if (series == 1) {
							Common.alertFinish(context, "套餐只能删除，不能修改！");
							/***
							FragmentSetMeal Hit = new FragmentSetMeal();
							Bundle args = new Bundle();
							args.putString("code", bhd.getMenucode());
							args.putDouble("amount", bhd.getAmount());
							Hit.setArguments(args);
							ft.replace(R.id.left, Hit);
							***/
						} else {
							FragmentHit Hit = new FragmentHit();
							Bundle args = new Bundle();
							args.putString("code", bhd.getMenucode());
							args.putDouble("amount", bhd.getAmount());
							Hit.setArguments(args);
							ft.replace(R.id.left, Hit);
						}

						ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
						ft.addToBackStack(null);
						ft.commit();

					}
				});

				Button delete = new Button(context);
				delete.setText(R.string.delete);
				delete.setTextSize(10);
				delete.setHeight(15);
				delete.setWidth(30);
				delete.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// 鍔ㄦ�鍒犻櫎璁板綍
						table.removeView(row);

						// 鍔ㄦ�鍒犻櫎鍙宠竟鑿滃崟鏁伴噺

						for (Menu mu : SO.getMenuList()) {							 
							if (mu.getCode().equals(bhd.getBig_Menu_code())) {
								
								mu.setCount(mu.getCount()
										- (int) bhd.getAmount());								 
							}							
							if (bhd.getRljtype() == mu.getC1()) {
								mu.setCount(mu.getCount()
										- (int) bhd.getAmount());
							}
						}
						list.remove(bhd);
						((FragmentMAin) context).refreshRight();
					}
				});
				row.addView(menu, new TableRow.LayoutParams());
				row.addView(price, new TableRow.LayoutParams());
				row.addView(amount, new TableRow.LayoutParams());
				row.addView(tag, new TableRow.LayoutParams());
				row.addView(total, new TableRow.LayoutParams());
				row.addView(callmenu, new TableRow.LayoutParams());
				row.addView(edit, new TableRow.LayoutParams());
				row.addView(delete, new TableRow.LayoutParams());

				table.addView(row, new TableLayout.LayoutParams());
			}

		}

		totalamount.setText(Math.round(tt) + "");
		vipprice.setText(Math.round(tp * 0.8) + "");
	}

}
